package com.fddh.springboot.controller;

import com.fddh.springboot.dto.NewUserDto;
import com.fddh.springboot.dto.ResponseDto;
import com.fddh.springboot.dto.UserDto;
import com.fddh.springboot.entity.User;
import com.fddh.springboot.enums.YesNoEnum;
import com.fddh.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

//@CrossOrigin解决前后端跨域问题
@CrossOrigin
@Controller
@RequestMapping(value="/userAdmin")
public class UserController {

    @Autowired
    @Qualifier("UserServiceImpl") //取得bean，指定bean名称为LoginServiceImpl
    private UserService userService;

    /**
     * 获取用户列表数据
     */
    //getUserList
    @ResponseBody
    @RequestMapping(value = "/getUserList")
    public ResponseDto getUserList() {

        //ResponseDto对应返回给前端的数据
        ResponseDto responseDto = new ResponseDto();

        List<User> list = new ArrayList<>();
        list = userService.getUserList();
        //将list数据放到responseDto的Content中
        responseDto.setContent(list);
        //返回数据responseDto给前端
        return responseDto;
    }

    /**
     * 添加用户模块，返回添加成功flag标志。目前不能重复添加用户
     */
    //addUser
    @ResponseBody
    @RequestMapping(value = "/addUser")
    //add,NewUserDto对应用户请求携带的数据类型
    public ResponseDto addUser(@RequestBody NewUserDto newuserDto, HttpServletRequest request) {

        //添加是否成功的标志
        int flag = 0;
        flag = userService.addUser(newuserDto);

        //ResponseDto对应返回给前端的数据
        ResponseDto responseDto = new ResponseDto();

        if(flag!=0){
            responseDto.setCode(YesNoEnum.YES);//添加成功
        }else{
            responseDto.setCode(YesNoEnum.NO);
        }
        return responseDto;
    }

    /**
     * 删除用户，返回删除成功flag标志
     */
    //deleteUser
    @ResponseBody
    @RequestMapping(value = "deleteUser")
    public ResponseDto deleteUser(@RequestBody UserDto userDto, HttpServletRequest request) {

        //ResponseDto对应返回给前端的数据
        ResponseDto responseDto = new ResponseDto();

        //返回是否删除成功的标志
        int flag = 0;
        flag = userService.deleteUser(userDto);

        if(flag!=0){
            responseDto.setCode(YesNoEnum.YES);//删除成功
        }else{
            responseDto.setCode(YesNoEnum.NO);
        }
        return responseDto;
    }

    /**
     * 修改用户信息。思路：用户的username是不能变的，通过username去判断需要修改的是谁
     */
    //updateUser
    @ResponseBody
    @RequestMapping(value = "updateUser")
    public String updateUser(@RequestBody NewUserDto newUserDto, HttpServletRequest request) {

        //NewUserDto对应返回给前端的数据
        //前端提供的username要和数据库的一致，不然sql找不到这个人
        userService.updateUser(newUserDto);

        //当前这个修改成功与否并没有去做真实的判断
        return "update success!";

    }
}

